package accessDBProj;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Utilities {
	public static boolean instance = false;// 单实例标志

	/**
	 * 功能：实现结果集的表格显示
	 */
	public static void displayResultSet(JTable table_inst, Vector columnHeads,
			Vector table_rows) throws SQLException {
		Vector rows = vectorClone(table_rows);
		Vector columnHeads_clone=(Vector) columnHeads.clone();
		columnHeads_clone.addElement("tag");
		for (int i = 0; i < rows.size(); i++) {
			((Vector) rows.get(i)).set(9, Integer.toString(i));
		}
		try {
			MyTableModel model = new MyTableModel(rows, columnHeads_clone);
			table_inst.setModel(model);
			return;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/*
	 * Vector对象深度克隆
	 */
	private static Vector vectorClone(Vector orig) {
		Vector obj = new Vector();
		for (int i = 0; i < orig.size(); i++) {

			Vector temp = (Vector) ((Vector) orig.get(i)).clone();// 子元素clone

			obj.add(temp);

		}

		return obj;
	}
}

/*
 * 重写isCellEditable方法，使单元格不可编辑
 */
class MyTableModel extends DefaultTableModel {

	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public MyTableModel(Vector data, Vector columns) {
		super(data, columns);
	}
}
